package com.aplink.generic.widgets;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.CheckBoxSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.util.ResourcesHelper;

public class ApLinkCheckBox extends ApLinkBaseView {
    /**
     * 
     */
    private static final long serialVersionUID = -5066801563603684846L;
    private View group;
    private final OnCheckedChangeListener mChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(final CompoundButton buttonView,
                final boolean isChecked) {
            if (ApLinkCheckBox.this.mCheckBoxSpec.isToggleOfflineMode()) {
                GenericSharePreference.getInstance().setOfflineMode(isChecked);
            } else if (ApLinkCheckBox.this.mCheckBoxSpec.isToggleAutoSync()) {
                GenericSharePreference.getInstance().setEnableAutoSync(
                        isChecked);
            } else {
                ApLinkCheckBox.this
                        .toggle(ApLinkCheckBox.this.group, isChecked);
            }
            ApLinkCheckBox.this.handleTextMode(isChecked);
        }
    };
    private final CheckBox mCheckBox;

    private final CheckBoxSpec mCheckBoxSpec;

    public ApLinkCheckBox(final GenericFragment genericFragment,
            final ControlSpec controlSpec, final View view) {
        super(genericFragment, controlSpec, view);
        this.mCheckBox = (CheckBox) view;
        this.mCheckBoxSpec = (CheckBoxSpec) controlSpec;
        if (this.mCheckBoxSpec.getDisableGroupID() != null) {
            final int viewID = ResourcesHelper
                    .getViewIdByName(this.mCheckBoxSpec.getDisableGroupID());
            this.group = getActivity().findViewById(viewID);
        }
    }

    @Override
    public void clear() {

    }

    @Override
    public GenericAttribute getAttribute() {
        final String column = this.mCheckBoxSpec.getFullColumnName();
        final ObjPropInfo objPropInfo = getGenericFragment().getContentData()
                .get(column);
        String value = null;
        if (this.mCheckBox.isChecked()) {
            value = this.mCheckBoxSpec.getValueOn();
        } else {
            value = this.mCheckBoxSpec.getValueOff();
        }
        return GenericAttribute.obtain(value, objPropInfo);
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public View getView() {
        return this.mCheckBox;
    }

    private void handleTextMode(final boolean isChecked) {
        if (isChecked) {
            if (!TextUtils.isEmpty(this.mCheckBoxSpec.getTextOn())) {
                this.mCheckBox.setText(this.mCheckBoxSpec.getTextOn());
            }
        } else {
            if (!TextUtils.isEmpty(this.mCheckBoxSpec.getTextOff())) {
                this.mCheckBox.setText(this.mCheckBoxSpec.getTextOff());
            }
        }
    }

    @Override
    protected void initViewProperties() {
        if (this.mCheckBoxSpec.isToggleOfflineMode()) {
            final boolean isOfflineMode = GenericSharePreference.getInstance()
                    .isOfflineMode();
            this.mCheckBox.setChecked(isOfflineMode);
        } else if (this.mCheckBoxSpec.isToggleAutoSync()) {
            final boolean isAutoSync = GenericSharePreference.getInstance()
                    .isEnableAutoSync();
            this.mCheckBox.setChecked(isAutoSync);
        }
        handleTextMode(this.mCheckBox.isChecked());
    }

    @Override
    protected void loadBindingObject() {
        if (!this.mCheckBoxSpec.isToggleOfflineMode()) {
            final String value = getHashPassedValues().get(
                    this.mCheckBoxSpec.getFullColumnName());
            final boolean isChecked = value.equalsIgnoreCase(this.mCheckBoxSpec
                    .getValueOn());
            toggle(this.group, isChecked);
        }
    }

    @Override
    protected void loadDefaultObject() {
        if (!this.mCheckBoxSpec.isToggleOfflineMode()
                && !this.mCheckBoxSpec.isToggleAutoSync()) {
            toggle(this.group, this.mCheckBox.isChecked());
        }
    }

    @Override
    protected void setEventListener() {
        this.mCheckBox.setOnCheckedChangeListener(this.mChangeListener);
    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    private void toggle(final View view, final boolean isChecked) {
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                toggle(((ViewGroup) view).getChildAt(i), isChecked);
            }
        } else {
            if (this.mCheckBoxSpec.isDisable()) {
                view.setEnabled(isChecked);
            } else {
                if (isChecked) {
                    view.setVisibility(View.GONE);
                } else {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }
    }

}
